package own.stu.jobgib.playown.fsm.easyStates.core;

import own.stu.jobgib.playown.fsm.easyStates.api.FiniteStateMachine;
import own.stu.jobgib.playown.fsm.easyStates.api.State;
import own.stu.jobgib.playown.fsm.easyStates.common.Utils;

import java.util.Set;

class FiniteStateMachineDefinitionValidator {

    /**
     * Deterministic FSM validation :
     * for each state, exactly one outgoing transition for an event type must be defined.
     */
    void validateFiniteStateMachineDefinition(FiniteStateMachine finiteStateMachine) {

        Set<State> states = finiteStateMachine.getStates();

        //check if initial state belongs to FSM declared states.
        State initialState = finiteStateMachine.getInitialState();
        if (!states.contains(initialState)) {
            throw new IllegalStateException("Initial state '" + initialState.getName() + "' must belong to FSM states: " +
                    Utils.dumpFSMStates(states));
        }

        //check if registered final states belong to FSM declared states.
        for (State finalState : finiteStateMachine.getFinalStates()) {
            if (states.contains(finalState)) {
                continue;
            }
            throw new IllegalStateException("Final state '" + finalState.getName() + "' must belong to FSM states: " +
                    Utils.dumpFSMStates(states));

        }
    }
}
